<template>
  <view class="content screen-width">
    <view class="title_head">
      <view class="title_search">
        <view class="search_item">
          <input type="text" placeholder="请输入您想要查询的关键词" v-model="searchKey" />
          <image @tap="getList(1)" src="../../static/proty/search_icon.png" />
        </view>
      </view>
    </view>
    <view class="areaList">
      <view @tap="showtime">
        <input type="text" :value="time ? time : '发布时间'" />
        <image src="../../static/proty/botton_icon.png" />
      </view>
    </view>

    <scroll-view scroll-y class="content_main" @scrolltolower="scrolltolower">
      <ul>
        <!-- 成果 -->
        <navigator
          redirect
          :url="`/pages/fileDownload/fileDetail?id=${item.id}`"
          v-for="(item, index) in list"
          :key="index"
          class="listItem"
        >
          <view class="title" v-html="item.fileName"></view>
          <view class="center flex items-middle space-between">
            <view class="time multipleRow">下载次数：{{ item.downloadNumber || '0' }}</view>
            <view class="time multipleRow">{{ item.createTime || '--' }}</view>
          </view>
          <img @click.stop="down(item)" src="@/static/down.png" />
        </navigator>
      </ul>
      <!-- 没有数据的提示 -->
      <van-divider>我也是有底线的</van-divider>
    </scroll-view>
    <!-- 发布时间 -->
    <van-popup v-model="isshowTime" position="bottom" :style="{ height: '50%' }">
      <van-datetime-picker
        v-model="currentDate"
        type="date"
        title="选择发布时间"
        :min-date="minDate"
        :max-date="maxDate"
        @confirm="onConfirmTimes"
        @cancel="cancel"
      />
    </van-popup>
    <Festival></Festival>
  </view>
</template>

<script>
import { fileList, mobileDownload } from "../../util/api/file.js";
import { isWeixin, wxShareConfig } from "@/util/wx.js";
import { imgUrl } from "@/util/api.js";
export default {
  data() {
    return {
      list: [],
      searchKey: "",

      total: 0,
      pageNo: 1,

      isshowTime: false,
      time: "", //展示数据(时间)

      minDate: new Date(2000, 0, 1),
      maxDate: new Date(2025, 10, 1),
      currentDate: new Date()
    };
  },
  onLoad() {
    this.getList(1);
  },
  methods: {
    down(item) {
      let token = this.$getToken();
      if (!token) {
        uni.showToast({
          title: "您还没有登录，请登录后操作",
          duration: 2000,
          icon: "none"
        });
        setTimeout(() => {
          uni.setStorageSync("pageUrl", window.location.href);
          uni.navigateTo({
            url: "/pages/login/index"
          });
        }, 2000);
        return;
      }
      mobileDownload(item.id).then(res => {
        if (res.code == 200 && res.result) {
          window.location.href = imgUrl + res.result;
        } else {
          uni.showToast({
            title: "下载失败，请重试！",
            duration: 2000,
            icon: "none"
          });
        }
      });
    },
    getList(type) {
      if (type === 1) {
        this.pageNo = 1;
      }
      let params = {
        pageNo: this.pageNo,
        pageSize: 10,
        fileName: this.searchKey,
        createTime: this.time
      };
      fileList(params).then(res => {
        if (this.pageNo === 1) {
          this.list = res.result.records;
        } else {
          this.list.push(...res.result.records);
        }
        this.$changeText(res.result.records, "fileName", this.searchKey);
        this.total = res.result.total;
      });
    },

    cancel() {
      this.isshowTime = false;
    },
    showtime() {
      this.isshowTime = true;
    },
    onConfirmTimes(v) {
      this.time = this.timeFormat(v);
      this.cancel();
      this.getList(1);
    },
    timeFormat(time) {
      // 时间格式化 2019-09-08
      let year = time.getFullYear();
      let month = (time.getMonth() + 1).toString().padStart(2, "0");
      let day = time
        .getDate()
        .toString()
        .padStart(2, "0");
      return year + "-" + month + "-" + day;
    },
    scrolltolower() {
      if (this.list.length >= this.total) {
        return;
      }
      this.pageNo = this.pageNo + 1;
      this.getList();
    }
  }
};
</script>

<style lang="less" scoped>
.popup {
  height: fit-content;
  width: 100%;
  background: #fff;
}

.picker-btn {
  display: flex;
  height: 100upx;
  width: 100%;
  line-height: 100upx;
  background: #fff;
  font-size: 17px;
  z-index: 1;
  border-bottom: 1rpx solid #f8f8f8;

  .left {
    flex: 1;
    color: #0076ff;
    padding-left: 40upx;
    box-sizing: border-box;
  }

  .right {
    flex: 1;
    text-align: right;
    padding-right: 40upx;
    color: #fe4533;
    box-sizing: border-box;
  }
}

picker-view {
  width: 100%;
  height: 500rpx;
  display: relative;
}
.item {
  line-height: 100rpx;
  text-align: center;
}
.title_head {
  padding: 20rpx 30rpx;
  background-color: #2862ad;

  .title_item {
    display: flex;
    align-items: center;
    height: 100rpx;

    ul {
      display: flex;
      align-items: center;
      width: 100%;
      li {
        position: relative;
        font-size: 14px;
        color: #aaa;
        text-align: center;
        width: 30%;
        &.active {
          color: #fff;
          font-size: 16px;
          &::before {
            content: "";
            position: absolute;
            left: 40%;
            bottom: -25rpx;
            width: 30rpx;
            height: 3px;
            border-radius: 7rpx;
            background-color: #44a2ff;
          }
        }
      }
    }
  }

  .title_search {
    padding: 20rpx auto;
    height: 120rpx;
    align-items: center;
    display: flex;

    .search_item {
      height: 40px;
      width: 100%;
      display: flex;
      background-color: #fff;
      align-items: center;
      border-radius: 14rpx;
    }

    input {
      flex: 1;
      font-size: 14px;
      padding: 0 10rpx;
      padding-left: 30rpx;
      height: 30rpx;
      &::placeholder {
        font-size: 12px;
      }
    }

    image {
      max-width: 16px;
      max-height: 16px;
      margin: 0 30rpx;
    }
  }
}

.content_main {
  height: calc(100vh - 380rpx);
  padding-bottom: 60rpx;
  ul {
    .listItem {
      border-radius: 10rpx;
      background-color: #fff;
      margin: 20rpx 30rpx;
      padding: 30rpx 40rpx;
      position: relative;
      .title {
        color: #000;
        font-weight: 700;
        font-size: 16px;
        margin-bottom: 20upx;
      }
      .center {
        color: #aaa;
        font-size: 10px;
      }
      img {
        position: absolute;
        top: 6rpx;
        right: 8rpx;
      }
    }
  }
}

.areaList {
  padding: 0 20rpx;
  box-sizing: border-box;
  display: flex;
  align-items: center;
  height: 60px;
  overflow: scroll;
  view {
    max-height: 80rpx;
    background-color: #fff;
    margin-right: 30rpx;
    display: flex;
    padding: 10rpx 10rpx;
    align-items: center;
    width: 27%;

    image {
      width: 20rpx;
      height: 14rpx;
      margin-left: 20rpx;
    }

    input {
      width: 140rpx;
      font-size: 10px;
      color: #44a2ff;
    }
  }
}

/deep/ .van-dropdown-menu__bar {
  display: flex;
  align-items: center;
  background-color: #f8f9fd;
  padding-left: 30rpx;
}

/deep/ .van-dropdown-menu__item {
  flex: none;
  padding: 10rpx 30rpx;
  text-align: center;
  height: 56rpx;
  background-color: #fff;
  margin-right: 20rpx;

  span {
    color: #77b0ff;
    font-size: 14px;
  }
}
</style>

